package kr.hkit.password.dao;

import java.util.Random;

public class ChangePassword {
	private String pw;
	private int[] c;
	private int[] count;
	private String check;
	
	public ChangePassword(String pw) {
		super();
		this.pw = pw.toLowerCase();
		this.c = getC();
		this.count = new int[]{ 0, 0, 0 };
		getCount();
	}

	public ChangePassword() {
		super();
	}

	private int[] getC() {
		c = new int[pw.length()];
		for (int i = 0; i < c.length; i++) {
			c[i] = pw.charAt(i);
		}
		return c;
	}

	public int[] getCount() {
		for (int i = 0; i < c.length; i++) {
			if (c[i] > 47 && c[i] < 58) {
				count[0]++;
			} else if (c[i] > 96 && c[i] < 123) {
				count[1]++;
			} else {
				count[2]++;
			}
		}
		return count;
	}

	public String getCheck() {
		int zeroCount = 0;
		if (c.length < 8) zeroCount = 2;
		for (int i = 0; i < count.length; i++) {
		 
			if (count[i] == 0) {
				zeroCount++;
			}
		}
		switch (zeroCount) {
		case 0:
			check = "안전";
			break;
		case 1:
			check = "보통";
			break;
		default:
			check = "위험";
			break;
		}
		return check;
	}

	public String getRandomPw() {
		Random rnd = new Random();
		pw = "";
		int r = 0;
		for (int i = 0; i < 12; i++) {
			r = rnd.nextInt(93) + 33;
			pw += (char)r;
		}
		
		c = getC();
		count = new int[]{ 0, 0, 0 };
		getCount();
		if (getCheck() != "안전") {
			getRandomPw();
		}
		return pw.toLowerCase();
	}
	
	
}
